﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;

namespace Battle.Net.Model
{
    [DataContract]
    public class CharacterItems : IEnumerable<Item>
    {

        public CharacterItems()
        {
            Items = new List<Item>();
        }

        [DataMember(Name = "averageItemLevel")]
        public int AverageItemLevel { get; set; }
        [DataMember(Name = "averageItemLevelEquipped")]
        public int AverageItemLevelEquipped { get; set; }
        [DataMember(Name = "back")]
        public Item Back { get; set; }
        [DataMember(Name = "chest")]
        public Item Chest { get; set; }
        [DataMember(Name = "feet")]
        public Item Feet { get; set; }
        [DataMember(Name = "finger1")]
        public Item Finger1 { get; set; }
        [DataMember(Name = "finger2")]
        public Item Finger2 { get; set; }
        [DataMember(Name = "hands")]
        public Item Hands { get; set; }
        [DataMember(Name = "legs")]
        public Item Legs { get; set; }
        [DataMember(Name = "mainHand")]
        public Item MainHand { get; set; }
        [DataMember(Name = "offHand")]
        public Item OffHand { get; set; }
        [DataMember(Name = "neck")]
        public Item Neck { get; set; }
        [DataMember(Name = "shoulder")]
        public Item Shoulder { get; set; }
        [DataMember(Name = "trinket1")]
        public Item Trinket1 { get; set; }
        [DataMember(Name = "trinket2")]
        public Item Trinket2 { get; set; }
        [DataMember(Name = "waist")]
        public Item Waist { get; set; }
        [DataMember(Name = "wrist")]
        public Item Wrist { get; set; }

        private List<Item> Items { get; set; }


        private void PopulateItems()
        {
            if (Items == null)Items = new List<Item>();
            Items.Clear();

            var t = typeof (CharacterItems);
            var properties = t.GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.PropertyType == typeof(Item)).Select(p => p.GetGetMethod());
            foreach (var mi in properties)
            {
                var item = (Item)mi.Invoke(this, null);
                if (item != null)
                    Items.Add(item);
            }

        }

        public IEnumerator<Item> GetEnumerator()
        {
            PopulateItems();
            return Items.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            PopulateItems();
            return Items.GetEnumerator();
        }
    }
}